package cc.fine.svc.main.service.qiniu.impl;

import cc.fine.svc.main.properties.QiniuKodoProperties;
import cc.fine.svc.main.service.qiniu.QiniuKodoService;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.DownloadUrl;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.persistent.FileRecorder;
import com.qiniu.util.Auth;
import com.qiniu.util.IOUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;

/**
 * @AUTHOR: wyihe
 * @DATE: 2023/10/25
 */
@Service
@Slf4j
public class QiniuKodoServiceImpl implements QiniuKodoService {

    @Override
    public String upload(MultipartFile mfile) {



        InputStream file = null;
        String fileKey = "";


        // 上传部分
        try {
            file = mfile.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.huanan());
        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本
        cfg.resumableUploadMaxConcurrentTaskCount = 2;  // 设置分片上传并发，1：采用同步上传；大于1：采用并发上传

        String accessKey = QiniuKodoProperties.AK;
        String secretKey = QiniuKodoProperties.SK;
        String bucket = QiniuKodoProperties.BUCKET;

        String key = null;

        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);

        String localTempDir = Paths.get(System.getenv("java.io.tmpdir"), bucket).toString();
        try {
            //设置断点续传文件进度保存目录
            FileRecorder fileRecorder = new FileRecorder(localTempDir);
            UploadManager uploadManager = new UploadManager(cfg, fileRecorder);
            try {
                Response response = uploadManager.put(IOUtils.toByteArray(file), key, upToken);
                //解析上传成功的结果
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                fileKey = putRet.key;
//                System.out.println(putRet.key);
//                System.out.println(putRet.hash);
            } catch (QiniuException ex) {
                ex.printStackTrace();
                if (ex.response != null) {
                    System.err.println(ex.response);

                    try {
                        String body = ex.response.toString();
                        System.err.println(body);
                    } catch (Exception ignored) {
                    }
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }


//        下载部分
        DownloadUrl url = new DownloadUrl(QiniuKodoProperties.DOWNLOAD_URL, false, fileKey);

        String urlString = null;
        try {
            urlString = url.buildURL();
        } catch (QiniuException e) {
            throw new RuntimeException(e);
        }
        return urlString;

    }
}
